package com.enjoy.redis.pipeline;

import org.junit.Test;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;

import java.util.Random;

public class PipelineTest {

    @Test
    public void test1() {
        Jedis jedis = new Jedis(new HostAndPort("192.168.126.128", 6379));
        jedis.flushDB();
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            int j = new Random().nextInt(1000);
            jedis.set("key" + i, "value" + j);

        }
        System.out.println("test1耗时:" + (System.currentTimeMillis() - start)+"ms");//520ms
    }

    @Test
    public void test2() {
        Jedis jedis = new Jedis(new HostAndPort("192.168.126.128", 6379));
        jedis.flushDB();
        Pipeline pipelined = jedis.pipelined();
        long start = System.currentTimeMillis();
        for (int i = 1000; i < 2000; i++) {
            int j = new Random().nextInt(1000);
            pipelined.set("key" + i, "value" + j);
        }

        System.out.println("test2耗时:" + (System.currentTimeMillis() - start)+"ms");
    }

}
